package com.company.algo.niuke;

/**
 * 最长公共前缀
 * 编写一个函数来查找字符串数组中的最长公共前缀。
 */
public class LongestCommonPrefix {
    public String longestCommonPrefix (String[] strs) {
        if (strs.length == 0) return "";
        String prefix = strs[0];
        for (int i = 1; i < strs.length; i++) {
            prefix = findComm(prefix,strs[i]);
            if (prefix.length()==0) break;
        }
        return prefix;
    }
    public String findComm(String str1,String str2){
        int len = Math.min(str1.length(),str2.length());
        int i = 0;
        while (i<len){
            if (str1.charAt(i)!=str2.charAt(i)) break;
            i++;
        }
        return str1.substring(0,i);
    }
}
